﻿using System;
using System.Collections.Generic;
using Go.Tools;

namespace Go.Model
{
    class BoardFactory
    {
        const int MAXNNEIGHTBORS = 4;

        public static Dictionary<Coordinate,Position> createBoard(int size) 
        {
            Dictionary<Coordinate, Position> board = BoardFactory.createPositions(size);

            BoardFactory.setNeighbors( board.Values);
            return board;
        }

        private static Dictionary<Coordinate, Position> createPositions(int size)
        {
            Dictionary<Coordinate, Position> board = new Dictionary<Coordinate, Position>();
            for (int row = 0; row < size; row++)
            {
                for (int column = 0; column < size; column++)
                {
                    Coordinate aCoordinate = Functions.getCoordinate(row, column);
                    Position aNewPostion = new Position(aCoordinate);
                    board.Add(aCoordinate,aNewPostion);
                }
            }
            return board;
        }

        private static void setNeighbors(Dictionary<Coordinate, Position>.ValueCollection board)
        {

            foreach (Position aPosition in board )
            {
                BoardFactory.setNeighbors(board, aPosition);
            }
        }

        //Setea los vecinos de la posicion pasada por parametro con las otras posiciones del tablero
        private static void setNeighbors(Dictionary<Coordinate, Position>.ValueCollection board, Position aPosition)
        {
            foreach (Position otherPosition in board)
            {
                if (aPosition.itMustBeNeighbor(otherPosition))
                {
                    aPosition.addNeighborPosition (otherPosition);
                }
                if (aPosition.NeighborPositions.Count == MAXNNEIGHTBORS)
                    return;
            }
        }
    }
}
